package selfview.sufutian.com.myapplication.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by sufutian on 2018/1/19.
 */

public class BitmapUtil {
    private static final BitmapUtil ourInstance = new BitmapUtil();

    public static BitmapUtil getInstance() {
        return ourInstance;
    }

    private BitmapUtil() {

    }

    /**
     * 压缩图片
     *
     * @param picturePath 本地图片路径
     * @return
     */
    public static boolean compressLocalPicture(String picturePath) {

        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inSampleSize = 4;
        op.inJustDecodeBounds = true;
        Bitmap pic = null;
        pic = BitmapFactory.decodeFile(picturePath, op);  //先从本地读照片，然后利用op参数对图片进行处理

        //将处理后的图片重新写回本地t
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(picturePath);
            if (pic != null) {
                pic.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
            }
            outputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * bitmap 质量压缩
     */
    public static Bitmap compressQuality(Bitmap bitmap, int quality) {
        Bitmap bm;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        byte[] bytes = baos.toByteArray();
        bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)
                + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight()
                + "bytes.length=  " + (bytes.length / 1024) + "KB"
                + "quality=" + quality);
        return bm;

    }

    /**
     * 本地图片 采样率压缩
     *
     * @param path
     * @param inSampleSize
     * @return
     */
    public static Bitmap compressSimpleSize(String path, int inSampleSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bm;
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = true;
        bm = BitmapFactory.decodeFile(path, options);
        Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)
                + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());
        return bm;
    }

    /**
     * bitmap 缩放宽高
     *
     * @param bitmap
     * @return
     */
    public static Bitmap compressScale(Bitmap bitmap) {
        Bitmap bm;
        Matrix matrix = new Matrix();
        matrix.setScale(0.5f, 0.5f);
        bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
        Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)
                + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());
        return bm;
    }

    /**
     * bitmap 压缩指定尺寸
     *
     * @param bitmap
     * @param width
     * @param height
     * @return
     */
    public static Bitmap compressSize(Bitmap bitmap, int width, int height) {
        Bitmap bm;
        bm = Bitmap.createScaledBitmap(bitmap, width, height, true);
        Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024) + "KB宽度为"
                + bm.getWidth() + "高度为" + bm.getHeight());
        return bm;
    }

    /**
     * bitmap  转 file
     *
     * @param bitmap
     * @param outputPath
     */
    public boolean saveBitmapFile(Bitmap bitmap, String outputPath) {
        File file = new File(outputPath);//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
